package com.example.mydy.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.Task;

import java.util.List;

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder> {
    private List<Task> tasks;
    private TaskListener listener;

    public interface TaskListener {
        void onTaskSelected(Task task);
    }

    public TaskAdapter(List<Task> tasks, TaskListener listener) {
        this.tasks = tasks;
        this.listener = listener;
    }

    @NonNull
    @Override
    public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_task_review, parent, false);
        return new TaskViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
        Task task = tasks.get(position);
        
        holder.tvTitle.setText(task.getTitle());
        holder.tvDescription.setText(task.getDescription());
        holder.tvUsername.setText("提交者: " + task.getUsername());
        holder.tvReward.setText(String.format("奖励: ¥%.2f", task.getReward()));
        
        // 设置操作按钮
        holder.btnApprove.setOnClickListener(v -> {
            if (listener != null) {
                listener.onTaskSelected(task);
            }
        });
        
        // 整个项目的点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onTaskSelected(task);
            }
        });
    }

    @Override
    public int getItemCount() {
        return tasks != null ? tasks.size() : 0;
    }

    static class TaskViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvDescription;
        TextView tvUsername;
        TextView tvReward;
        Button btnApprove;

        public TaskViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_task_title);
            tvDescription = itemView.findViewById(R.id.tv_task_description);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvReward = itemView.findViewById(R.id.tv_reward);
            btnApprove = itemView.findViewById(R.id.btn_view_task);
        }
    }
} 